package com.example.controller;


import com.example.model.auto.Course;
import com.example.model.auto.User;
import com.example.service.ICourseService;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseController {

    @Resource
    private ICourseService iCourseService;

    /*查询课程*/
    @GetMapping("/getCourse")
    public List<Course> getCourse(){
        return iCourseService.list();
    }

    /*通过id查询课程*/
    @GetMapping("/getCourseById/{id}")
    public Course getCourseById(@PathVariable("id") int id){
        return iCourseService.getById(id);
    }

    /*修改课程*/
    @PostMapping("/updateCourse")
    public void updateCourse(@RequestBody Course course){
        iCourseService.updateById(course);
    }

    /*添加课程*/
    @PostMapping("/setCourse")
    public void setCourse(@RequestBody Course course){
        System.out.println(course);

        iCourseService.save(course);
    }

    /*删除课程*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){
        System.out.println(id);
        return iCourseService.removeById(id);
    }


    /*图片上传*/
    @PostMapping("/elupload")
    public String elUpload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        File dest = new File("D:\\project\\study\\vue\\static\\Course_img\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
            return "上传失败！";
    }
}
